//
//  NetworkHandler.m
//  封装网络请求
//  MaxMovie
//
//  Created by Maxdong on 14-12-24.
//  Copyright (c) 2014年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "NetworkHandler.h"

@implementation NetworkHandler
- (void)getDataWithURLString:(NSString *)urlStr completion:(BLOCK)block{
    //1.先给自己的block属性赋值
    self.passValue = block;
    NSString *str = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.f];
    [request setHTTPMethod:@"GET"];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError){
        if (data != nil) {
            id object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            //block的使用 在数据取到之后 使用
            //解析的结果传给 试图控制器
            self.passValue(object);
           
        }
    }];

}
+ (void)connectioonNetGETDataWithURLString:(NSString *)urlStr completion:(BLOCK)block{
    NetworkHandler *netWork = [[NetworkHandler alloc] init];
    [netWork getDataWithURLString:urlStr completion:block];
    [netWork autorelease];
}
@end
